package com.abu.edums.service.impl;

import com.abu.edums.client.OrderClient;
import com.abu.edums.mapper.CourseMapper;
import com.abu.edums.service.ChapterService;
import com.abu.edums.utils.JwtUtils;
import com.abu.exceptionhandler.EduException;
import com.abu.pojo.Course;
import com.abu.vo.ChapterVO;
import com.abu.vo.CourseQueryVo;
import com.abu.vo.CourseWebVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CourseFrontService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private OrderClient orderClient;


    public Map<String, Object> queryFrontList(CourseQueryVo queryVo, Long page, Long limit) {
//        分页条件
        Page<Course> coursePage = new Page<>(page, limit);
//      查询条件
        String subjectId = queryVo.getSubjectId();
        String subjectParentId = queryVo.getSubjectParentId();
//        排序条件
        String priceSort = queryVo.getPriceSort();
        String buyCountSort = queryVo.getBuyCountSort();
        String gmtCreateSort = queryVo.getGmtCreateSort();

        QueryWrapper<Course> wrapper = new QueryWrapper<>();
//       条件非空则拼接
        if (StringUtils.isNotBlank(subjectId)) {
            wrapper.eq("subject_id", subjectId);
        }
        if (StringUtils.isNotBlank(subjectParentId)) {
            wrapper.eq("subject_parent_id", subjectParentId);
        }
        if (StringUtils.isNotBlank(priceSort)) {
            wrapper.orderByDesc("price");
        }
        if (StringUtils.isNotBlank(buyCountSort)) {
            wrapper.orderByDesc("buy_count");
        }
        if (StringUtils.isNotBlank(gmtCreateSort)) {
            wrapper.orderByDesc("gmt_create");
        }
//        执行查询
        Page<Course> pageParam = this.courseMapper.selectPage(coursePage, wrapper);
//        获取结果集
        List<Course> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();
//        封装结果集
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    public HashMap<String, Object> queryCourseWeb(String courseId, HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap<>();
//      1.查询课程信息
        CourseWebVo courseWebVo = this.courseMapper.queryCourseWebInfo(courseId);
//      2.相应查询章节和小节信息
        List<ChapterVO> chapterVideoList = this.chapterService.queryChapterAndBar(courseId);

//      3.远程调用根据课程id和用户id查询课程状态
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean buyCourseStatus = orderClient.isBuyCourse(memberId, courseId);

        if (StringUtils.isBlank(memberId)) {
            throw new EduException(20001, "请先登录");
        }
        map.put("chapterVideoList", chapterVideoList);
        map.put("courseWebVo", courseWebVo);
        map.put("isBuy", buyCourseStatus);
        return map;
    }
}
